package com.stylefeng.guns.rest.modular.auth.validator.impl;

import com.stylefeng.guns.core.cache.CacheKit;
import com.stylefeng.guns.rest.Util.Md5Util;
import com.stylefeng.guns.rest.common.persistence.dao.DriverInfMapper;
import com.stylefeng.guns.rest.common.persistence.model.DriverInf;
import com.stylefeng.guns.rest.modular.auth.validator.IReqValidator;
import com.stylefeng.guns.rest.modular.auth.validator.dto.Credence;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 账号密码验证
 *
 * @author fengshuonan
 * @date 2017-08-23 12:34
 */
@Service
public class DbValidator implements IReqValidator {

    private static  final String TRANSFERCACHE="CONSTANT";

    @Resource DriverInfMapper driverInfMapper;

    @Override
    public boolean validate(Credence credence) {
        String phone=credence.getCredenceName();
        String pass=credence.getCredenceCode();
        DriverInf u=new DriverInf();
        u.setDiPhone(phone);
        DriverInf driverInf=this.driverInfMapper.selectOne(u);
        if(driverInf!=null){
            String passWd=driverInf.getDiPassword();
            String salt=driverInf.getDiSalt();
            String opwd= Md5Util.md5(pass,salt);
      if (StringUtils.equals(passWd, opwd)) {
          driverInf.setDiClientId(credence.getClicode());
          driverInf.updateById();
          CacheKit.put(TRANSFERCACHE,phone,driverInf);
        return true;
        }
         }
        return false;
    }
}
